package cn.tedu.tea.service;

import cn.tedu.tea.domain.Menu;
import cn.tedu.tea.domain.param.MenuAddNewDTO;
import cn.tedu.tea.domain.param.MenuUpdateDTO;
import cn.tedu.tea.domain.vo.MenuListVO;
import cn.tedu.tea.domain.vo.MenuStandardVO;
import cn.tedu.tea.ex.ServiceException;
import cn.tedu.tea.mapper.MenuMapper;
import cn.tedu.tea.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class MenuServiceImpl implements IMenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public void addNew(MenuAddNewDTO menuAddNewDTO) {
        log.debug("开始处理【添加菜单】的业务，参数： {}", menuAddNewDTO);
        int count = menuMapper.countByTitle(menuAddNewDTO.getTitle());
        if (count > 0){
            String message = "添加菜单失败，标题已被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        Menu menu = new Menu();
        BeanUtils.copyProperties(menuAddNewDTO, menu);
        menu.setModifiedUserId(0L).setModifiedUserName("");
        int rows = menuMapper.insert(menu);
        if (rows != 1){
            String message = "添加菜单失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public void deleteById(Integer id) {
        log.debug("开始处理【根据id删除菜单】的业务，参数： {}", id);
        MenuStandardVO menuStandard = menuMapper.getStandardById(id);
        if (menuStandard == null){
            String message = "删除菜单失败，要删除的菜单不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        int rows = menuMapper.deleteById(id);
        if (rows != 1){
            String message = "删除菜单失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void deleteByIds(Integer[] ids) {
        log.debug("开始处理【根据id批量删除菜单】的业务，参数： {}", ids);
        int rows = menuMapper.deleteByIds(ids);
        if (rows < 1){
            String message = "删除菜单失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void updateInfoById(MenuUpdateDTO menuUpdateDTO) {
        log.debug("开始处理【修改菜单信息】的业务，参数：{}", menuUpdateDTO);
        MenuStandardVO menuStandard = menuMapper.getStandardById(menuUpdateDTO.getId());
        if (menuStandard == null){
            String message = "修改菜单失败，要访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        int count = menuMapper.countByTitleAndNotId(menuUpdateDTO.getTitle(), menuUpdateDTO.getId());
        if (count > 0){
            String message = "修改菜单失败，标题已被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        Menu menu = new Menu();
        BeanUtils.copyProperties(menuUpdateDTO, menu);
        int rows = menuMapper.update(menu);
        if (rows != 1){
            String message = "修改菜单失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public void setDisplay(Integer id) {
        log.debug("开始处理【将菜单设置为上架状态】的业务，参数：{}", id);
        updateStatusById(id, 0);
    }

    @Override
    public void setHidden(Integer id) {
        log.debug("开始处理【将菜单设置为下架状态】的业务，参数：{}", id);
        updateStatusById(id, 1);
    }

    @Override
    public MenuStandardVO getStandardById(Integer id) {
        log.debug("开始处理【根据id查询菜单详情】的业务，参数： {}", id);
        MenuStandardVO menuStandard = menuMapper.getStandardById(id);
        if (menuStandard == null){
            String message = "查询菜单详情失败，要查询的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return menuStandard;
    }

    @Override
    public List<MenuListVO> list() {
        log.debug("开始处理【查询菜单列表】的业务，无参数");
        List<MenuListVO> list = menuMapper.list();
        return list;
    }

    private void updateStatusById(Integer id, Integer status) {
        MenuStandardVO currentMenu = menuMapper.getStandardById(id);
        if (currentMenu == null) {
            String message = STATUS_TEXT[status] + "菜单失败，菜单数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        if (currentMenu.getStatus() == status) {
            String message = STATUS_TEXT[status] + "菜单失败，此菜单已经处于" + STATUS_TEXT[status] + "状态！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        Menu updateMenu = new Menu();
        updateMenu.setId(id);
        updateMenu.setStatus(status);
        int rows = menuMapper.update(updateMenu);
        if (rows != 1) {
            String message = STATUS_TEXT[status] + "菜单失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }
}
